package com.kitty.game.task.service.taskHandler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.npc.model.NpcButton;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.TaskDataPool;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.game.utils.Const;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class CampSelectionTaskHandler extends TaskHandler {
    private int minLevel = 130;

    /**接取第一个阵营任务*/
    public void handleRoleLevelUp(Role role, short level) {
        if (level < Const.MAX_LEVEL_NO_CAMP) {
            return ;
        }

        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        TaskSet firstMainTask = taskService.getTaskSet(TaskDataPool.CAMP_SELECTION, role);
        acceptTask(role, firstMainTask);
    }

    @Override
    public boolean resetTask(Role role, TaskSet taskSet) {
        /**阵营任务不能重置*/
        return false;
    }

    @Override
    public TaskType getTaskType() {
        return TaskType.CAMP_SELECTION;
    }

    @Override
    public String getTaskButtons(Role role, NPC npc) {
        /**满足以下条件时，需要显示阵营指引按钮
         *  1. 角色等级大于等于130
         *  2. 角色完成大飞任务
         *  3. 未完成当前任务
         */
        if (role.getLevel() < minLevel || role.getChildInfo().getType() < 3) {
            return "";
        }

        // 任务已完成，隐藏按钮
        if (role.getTask().isFinished(TaskDataPool.CAMP_SELECTION)) {return "";}

        TaskSet taskSet = taskService.getTaskSet(TaskDataPool.CAMP_SELECTION, role);
        String button = taskSet.getButtton();

        return button;
    }

    @Override
    public String getButtonContents(Role role, String content) {
        if (role.getLevel() < minLevel || role.getChildInfo().getType() < 3) {
            return "";
        }

        // 任务已完成，隐藏按钮
        if (role.getTask().isFinished(TaskDataPool.CAMP_SELECTION)) {return "";}

        String button = "";
        String[] buttonList = StringUtils.substringsBetween(content, "[", "]");
        for (String str : buttonList) {
            String prefix = NpcButton.CAMP_SELECTION_PREFIX.getKey() + String.valueOf(role.getChildInfo().getType());
            if (str.indexOf(prefix) != -1) {
                button += "[" + str + "]";
            }
        }

        return button;
    }

    @Override
    public boolean acceptTask(Role role, TaskSet taskSet) {
        if (!canAcceptTask(role, taskSet)) {
            return false;
        }

        doBeforeAccept(role, taskSet);

        doAcceptTask(role, taskSet);

        doAfterAccept(role, taskSet);

        return true;
    }

    @Override
    public void doBeforeAccept(Role role, TaskSet taskSet) {

    }
}
